package cn.lj.response;

import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.util.Map;


/**
 * @file:
 * @version:
 * @Description: 公共返回结果
 * 代码目的，作用，如何工作
 * @author: 李津
 * @date: 2021-04-13 15:07
 * 本代码要注意的事项、备注事项等
 */

@Data
public class Result {

    @ApiModelProperty(value = "是否成功")
    private Boolean success;

    @ApiModelProperty(value = "返回码")
    private Integer code;

    @ApiModelProperty(value = "返回消息")
    private String message;

    @ApiModelProperty(value = "返回数据")
    private Object data;


    private Result(Integer code, String message) {
        this.code = code;
        this.message = message;
    }

    /**
     * 构造方法私有化,里面的方法都是静态方法
     * 达到保护属性的作用
     */
    Result(){

    }

    /**
     * 链式编程
     * @return
     */
    public static Result ok(){
        Result result = new Result();
        result.setSuccess(true);
        result.setCode(ResultCode.SUCCESS.getCode());
        result.setMessage(ResultCode.SUCCESS.getMessage());
        return result;
    }

    public static Result error(){
        Result result = new Result();
        result.setSuccess(false);
        result.setCode(ResultCode.COMMON_FAIL.getCode());
        result.setMessage(ResultCode.COMMON_FAIL.getMessage());
        return result;
    }

    public static Result error(ResultCode resultCode){
        Result result = new Result();
        result.setSuccess(false);
        result.setCode(resultCode.getCode());
        result.setMessage(resultCode.getMessage());
        return result;
    }

    public static Result fail(String message) {
        return fail(400, message, null);
    }

    public static Result fail(int code, String message, Map<String,Object> map) {
        Result r = new Result();
        r.setCode(code);
        r.setMessage(message);
        r.setData(map);
        return r;
    }

    /**
     * 自定义返回成功与否
     * @param success
     * @return
     */
    public Result success(Boolean success){
        this.setSuccess(success);
        return this;
    }

    public Result message(String message){
        this.setMessage(message);
        return this;
    }

    public Result code(Integer code){
        this.setCode(code);
        return this;
    }

    public Result data(Object value){
        this.setData(value);
        return this;
    }

    public Result data(Map<String,Object> map){
        this.setData(map);
        return this;
    }

}
